/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.uml2tospmif;

import java.io.File;

/*import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
*/
import org.mitre.xmltransform.XPathNamespaceContextImpl;
import org.mitre.xmltransform.XmlInputDocument;
//import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Uml2InputDocument extends XmlInputDocument {
	public Uml2InputDocument(File pInputFile) {
		super(pInputFile);
	}

	protected void addNamespaces(XPathNamespaceContextImpl pNamespaces) {
		pNamespaces.addPrefix("UML", "org.omg.xmi.namespace.UML");
		pNamespaces.addPrefix("UML2", "org.omg.xmi.namespace.UML2");		
	}

	public String buildNodeName(Node pContext, String pFixedPrefix) {
		String name = this.getInputValue(pContext, "@name");
		if (name == null || name.length() == 0) {
			String namespace = pContext.getNamespaceURI();
			String prefix = this.getNamespacePrefix(namespace);
			String localName = pContext.getLocalName();
			String nodeName = ((prefix != null && prefix.length() > 0) ? prefix + ":" : "") + localName; 
			NodeList allNodes = this.getInputNodes("//" + nodeName);
			Node node;
			String xmiId;
			String resultPrefix = (pFixedPrefix != null) ? pFixedPrefix : localName;
			String nodeId = this.getInputValue(pContext, "@xmi.id");
			for (int i = 0; i < allNodes.getLength(); i++) {
				node = allNodes.item(i);
				xmiId = this.getInputValue(node, "@xmi.id");
				if (xmiId.equals(nodeId)) {
					return resultPrefix + (i+1);
				}
			}
			return resultPrefix + "_ID" + nodeId;
		}
		return name;
	}
}
